package com.project.springboot.mongodb.templates.configuration;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;

@Configuration
public class MongoDbConfiguration {

	@Value("${mongodb.uri}")
	private String mongodbUri;
	
	@Primary
	@Bean(name = "mongoTemplate")
	public MongoTemplate mongoTemplate() throws Exception {
		MappingMongoConverter converter = 
				new MappingMongoConverter(new DefaultDbRefResolver(mongoDbFactory()), new MongoMappingContext());
		converter.setTypeMapper(new DefaultMongoTypeMapper(null));
		return new MongoTemplate(mongoDbFactory(), converter);
	}
	
	@Bean
	@Primary
	public MongoDbFactory mongoDbFactory() {
		MongoClient client = new MongoClient(new MongoClientURI(mongodbUri));
		return new SimpleMongoDbFactory(client, "test");
	}
}
